package com.heima.bos.web.action.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller
public class ProcessInstanceAction extends ActionSupport{
	@Resource
	private ProcessEngine processEngine;
	//属性注入流程实例id
	private String id;
	/**
	 * 查询正在运行的流程实例
	 */
	public String list(){
		ProcessInstanceQuery query = processEngine.getRuntimeService()
											.createProcessInstanceQuery();
		List<ProcessInstance> list = query.list();
		ActionContext.getContext().getValueStack().set("list", list);
		return "list";
	}
	/**
	 * 根据id查询流程变量
	 * @throws IOException 
	 */
	public String findData() throws IOException{
		Map<String, Object> map = processEngine.getRuntimeService()
													.getVariables(id);
		ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
		ServletActionContext.getResponse().getWriter().print(map);
		return NONE;
	}
	/**
	 * 根据流程实例id查询实例数据
	 * @param id
	 * 获取deploymentId	imageName
	 */
	public String showPng(){
		String id = ServletActionContext.getRequest().getParameter("id");
		//获取流程实例对象
		ProcessInstance processInstance = processEngine.getRuntimeService().createProcessInstanceQuery()
											.processInstanceId(id).singleResult();
		String definitionId = processInstance.getProcessDefinitionId();
		//获取流程定义对象
		ProcessDefinitionEntity definition = (ProcessDefinitionEntity) processEngine.getRepositoryService().getProcessDefinition(definitionId);
		//获取流程部署id 和图片名称
		String deploymentId = definition.getDeploymentId();
		String imageName = definition.getDiagramResourceName();
		ActionContext.getContext().getValueStack().set("deploymentId", deploymentId);
		ActionContext.getContext().getValueStack().set("imageName", imageName);
		
		//获取activity对象
		String activityId = processInstance.getActivityId();
		ActivityImpl activity = definition.findActivity(activityId);
		
		int x = activity.getX();
		int y = activity.getY();
		int width = activity.getWidth();
		int height = activity.getHeight();
		
		ActionContext.getContext().getValueStack().set("x", x);
		ActionContext.getContext().getValueStack().set("y", y);
		ActionContext.getContext().getValueStack().set("width", width);
		ActionContext.getContext().getValueStack().set("height", height);
		return "showPng";
	}
	/**
	 * 根据流程部署id 和图片名称获取图片的流对象
	 * @param id
	 */
	//属性注入
	private String deploymentId,imageName;
	public String viewImage(){
		InputStream pngStream = processEngine.getRepositoryService().getResourceAsStream(deploymentId, imageName);
		ActionContext.getContext().getValueStack().set("pngStream", pngStream);
		return "viewImage";
	}
	public void setDeploymentId(String deploymentId) {
		this.deploymentId = deploymentId;
	}
	public void setImageName(String imageName) {
		this.imageName = imageName;
	}
	public void setId(String id) {
		this.id = id;
	}
}
